package com.eboss.sbs.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.config.Config;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.MessageDetailEntity;
import com.google.gson.reflect.TypeToken;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.views.MyDialog;

import java.util.HashMap;
import java.util.Map;

public class FeedBackActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_feedback);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "反馈与建议 ");
		
		ImageView img_call = (ImageView)findViewById(R.id.img_header_right);
		img_call.setVisibility(View.VISIBLE);
		img_call.setImageResource(R.mipmap.ic_call);
		img_call.setOnClickListener(this);
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		findViewById(R.id.tv_submit).setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.img_header_right:
				Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+ Config.TEL_CONTACT));
	            startActivity(intent); 
				break;
			case R.id.tv_submit:
				String content = getTvText(R.id.tv_content);
				if(StringUtil.isNull(content)){toast("反馈内容不能为空!");break;}
				else {feedback();}
				break;
			default:break;
		}
		super.onClick(v);
	}

	private void feedback() {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		String content = getTvText(R.id.tv_content);
		params.put("content", content);
		API.http_post(this, API.FEEDBACK_SUBMIT, params, new HttpCallback<MessageDetailEntity>(this) {
			@Override
			public void onSuccess(int statusCode, MessageDetailEntity data) {
				openConfirmDialog();
			}
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<MessageDetailEntity>() {
				};
			}
		});
	}
	
	private void openConfirmDialog(){
		MyDialog.Builder builder = new MyDialog.Builder(this);
		builder.setTitle("提示");
		builder.setMessage("您的反馈与建议我们已经收到，我们会尽快解决！");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				finish();
			}
		});
		builder.create().show();
	}
	
}
